package Homework4;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BoxLayout;

/**
 *
 * @author Jakim
 */
public class ButtonCounter extends Frame {

    // private fileds
    private Panel _panelOptions;
    private Panel _panelActions;

    public static CheckboxGroup _cbGroupAscDesc;
    public static CheckboxGroup _cbGroupStep;

    // public fields
    public static int sum;

    public Checkbox rbAscending;
    public Checkbox rbDescending;
    public Checkbox rbStep1;
    public Checkbox rbStep5;
    public Checkbox rbStep10;

    public Button btnClickMe;
    public Button btnClear;
    public static Label lblResult = new Label("0");

    //condtructor
    public ButtonCounter(){
        super("Button Counter");

        Label lblShif = new Label("");
        lblShif.setPreferredSize(new Dimension(100, 50));

        this.setLayout(new FlowLayout());

        this._cbGroupAscDesc = new CheckboxGroup();
        this._cbGroupStep = new CheckboxGroup();

        this.rbAscending = new Checkbox("Ascending", this._cbGroupAscDesc, true);
        this.rbDescending = new Checkbox("Descending", this._cbGroupAscDesc, false);
        this.rbStep1 = new Checkbox("Step1", _cbGroupStep, true);
        this.rbStep5 = new Checkbox("Step5", _cbGroupStep, false);
        this.rbStep10 = new Checkbox("Step10", _cbGroupStep, false);

        this._panelOptions = new Panel();
        this._panelOptions.setLayout(new BoxLayout(this._panelOptions, BoxLayout.Y_AXIS));
        this._panelOptions.add(this.rbAscending);
        this._panelOptions.add(this.rbDescending);
        this._panelOptions.add(lblShif);
        this._panelOptions.add(this.rbStep1);
        this._panelOptions.add(this.rbStep5);
        this._panelOptions.add(this.rbStep10);
        this._panelOptions.setSize(new Dimension(200, 300));

        this.btnClickMe = new Button("ClickMe");
        this.btnClickMe.setPreferredSize(new Dimension(100, 50));
        this.btnClickMe.addActionListener(new GetSum(_cbGroupAscDesc,
               _cbGroupStep));

        this.btnClear = new Button("Clear");
        this.btnClear.setPreferredSize(new Dimension(100, 50));
        this.btnClear.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                ButtonCounter.sum = 0;
                ButtonCounter.lblResult.setText("0");
            }
        });

        this._panelActions = new Panel();
        this._panelActions.setLayout(new BoxLayout(this._panelActions, BoxLayout.Y_AXIS));
        this._panelActions.add(this.btnClickMe);
        this._panelActions.add(lblResult);
        this._panelActions.add(this.btnClear);
        this._panelActions.setSize(new Dimension(120, 300));

        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        this.add(this._panelOptions);
        this.add(this._panelActions);
        this.pack();
        this.setVisible(true);
    }

    public static void main(String[] args){
        new ButtonCounter();
    }

    public class GetSum implements ActionListener{

        // private fields
        private CheckboxGroup _cbG1;
        private CheckboxGroup _cbG2;

        GetSum(CheckboxGroup cbG1, CheckboxGroup cbG2){
            this._cbG1 = cbG1;
            this._cbG2 = cbG2;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            ButtonCounter.sum = doCalculation(this._cbG1.getSelectedCheckbox(),
                    this._cbG2.getSelectedCheckbox());
            ButtonCounter.lblResult.setText(String.valueOf(ButtonCounter.sum));
        }

        private int doCalculation(Checkbox cbAction, Checkbox cbStep){
            int action = 0;
            int step = 0;

            if ((cbAction == null) && (cbStep == null)){
                return 0;
            }

            if (cbAction.getLabel().equals("Ascending")){
                action = 1;
            }else{
                action = -1;
            }

            if (cbStep.getLabel().equals("Step1")){
                step = 1;
            }else if (cbStep.getLabel().equals("Step5")){
                step = 5;
            }else if (cbStep.getLabel().equals("Step10")){
                step = 10;
            }

            return ButtonCounter.sum + action * step;
        }

    }
}
